package com.hframe.typefilter;

import org.apache.log4j.Logger;

public class ByteArrayToHex implements  FilterRef<String, Object> {
	static Logger log = Logger.getLogger(ByteArrayToHex.class);

	@Override
	public String filter(Object object) {
		if (object instanceof byte[]) {
			return toHexString((byte[]) object);
		}
		return null;
	}

	public static String toHexString(byte[] b) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < b.length; ++i) {
			String s = Integer.toHexString(b[i] & 0xFF);
			if (s.length() == 1)
				sb.append("0");

			sb.append(s.toUpperCase());
		}
		return sb.toString();
	}
}